package com.orcs.leefei.utils.kotlin

import android.content.Context
import android.content.pm.PackageManager
import android.support.v4.content.ContextCompat
import ru.noties.debug.Debug.v


/**
 *
 * Created by FeiLee on 2017/1/12 18:31.
 */
/** Determines if the context calling has the required permission
 * @param context - the IPC context
 * *
 * @param permissions - The permissions to check
 * *
 * @return true if the IPC has the granted permission
 */
fun hasPermission(context: Context, permission: String): Boolean {
    val res = ContextCompat.checkSelfPermission(context, permission)

    v {
        "permission: " + permission + " = \t\t" +
                if (res == PackageManager.PERMISSION_GRANTED) "GRANTED" else "DENIED"
    }

    return res == PackageManager.PERMISSION_GRANTED

}

/** Determines if the context calling has the required permissions
 * @param context - the IPC context
 * *
 * @param permissions - The permissions to check
 * *
 * @return true if the IPC has the granted permission
 */
fun hasPermissions(context: Context, vararg permissions: String): Boolean {

    var hasAllPermissions = true

    for (permission in permissions) {
        //return false instead of assigning, but with this you can log all permission values
        if (!hasPermission(context, permission)) {
            hasAllPermissions = false
        }
    }

    return hasAllPermissions

}